﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AB_SD_DataAccess;
using AB_SD_Entities;

namespace AB_SD_BusinessLogic
{
    public static class UserBUS
    {
        /// <summary>
        /// Check a user is exist or not
        /// </summary>
        /// <param name="username">username</param>
        /// <returns>return true if user is exist, else return false</returns>
        public static bool IsExist(string username)
        {
            User user = UserDAO.Select(username);
            return (user != null);
        }

        /// <summary>
        /// Check a user is superuser or not
        /// </summary>
        /// <param name="username">username</param>
        /// <returns>return true if user is superuser, else return false</returns>
        public static bool IsSuper(string username)
        {
            User user = UserDAO.Select(username);
            return user.IsSuper;
        }

        /// <summary>
        /// Check a login request if valid or not
        /// </summary>
        /// <param name="username">input username</param>
        /// <param name="password">input password</param>
        /// <returns>return true if it's valid, else return false</returns>
        public static bool IsValidLogin(string username, string password)
        {
            //if user is exist
            if (IsExist(username))
            {
                User user = UserDAO.Select(username);

                // if user's password is the same as input password, return true                
                // else, return false
                return (user.Password == password);
            }
            else // else, if user is not exist, return false
                return false;
        }

        /// <summary>
        /// Check if it's a valid password request
        /// </summary>
        /// <param name="username"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsValidPasswordRequest(string username, string email)
        {
            //if user is exist
            if (IsExist(username))
            {
                User user = UserDAO.Select(username);

                // if user's email is the same as input email, return true                
                // else, return false
                return (user.Email == email);
            }
            else // else, if user is not exist, return false
                return false;
        }

        /// <summary>
        /// Select a user by username
        /// </summary>
        /// <param name="username">username</param>
        /// <returns></returns>
        public static User Select(string username)
        {
            return UserDAO.Select(username);
        }
    }
}
